package com.atnjupt.admin.controller;

import com.atnjupt.admin.bean.Book;
import com.atnjupt.admin.bean.City;
import com.atnjupt.admin.bean.User;
import com.atnjupt.admin.service.BookService;
import com.atnjupt.admin.service.CityService;
import com.atnjupt.admin.service.impl.BookServiceImp;
import com.atnjupt.admin.service.impl.CityServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @author Wizarder
 * @create 2021-10-11 17:31
 */
@Controller
public class IndexController {
    //spring自带的一个操作数据库的轻量级模板
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private BookService bookService;

    @Autowired
    private CityService cityService;

    @GetMapping("/city")
    public String toCityPage(){
        return "city";
    }

    /**
     * mybatis测试
     * @param city
     * @return
     */
    @ResponseBody
    @PostMapping("/city")
    public City saveCity(City city){
        cityService.saveCity(city);
        return city;
    }

    /**
     * mybatis测试
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/city/{id}")
    public City getCity(@PathVariable("id") Integer id){
        return cityService.getCity(id);
    }

    /**
     * mybatis测试
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/book/{id}")
    public Book getBook(@PathVariable("id") Integer id){
        return bookService.getBook(id);
    }

    /**
     * 测试druid的一些配置
     * @return
     */
    @ResponseBody
    @GetMapping("/sql")
    public String jdbcTemplateSQL(){
        Long aLong = jdbcTemplate.queryForObject("select count(*) from t_book", Long.class);
        return aLong.toString();
    }
    /**
     * 跳转登录页面
     * @return
     */
    @GetMapping(value = {"/","/login"})
    public String toLogin(){
        return "login";
    }

    /**
     * 表单提交，重定向到首页，防止表单重复提交
     * @return
     */
    @PostMapping("/index")
    public String formToIndex(User user, Model model, HttpSession httpSession){
        if (StringUtils.isEmpty(user.getUsername()) ||StringUtils.isEmpty(user.getPassword())){
            model.addAttribute("msg","用户名或者密码为空");
            return "login";
        }else{
            if ("luhu".equals(user.getUsername()) && "admin".equals(user.getPassword())){
                httpSession.setAttribute("user",user);
                return "redirect:/index.html";
            }else{
                model.addAttribute("msg","用户名或者密码错误");
                return "login";
            }
        }

    }

    /**
     * 重定向到首页
     * @return
     */
    @GetMapping("/index.html")
    public String redirectIndex(HttpSession httpSession,Model model){
        //如果直接访问首页，核验是否有用户信息，没有就跳转登录页面
//        if (httpSession.getAttribute("user") == null){
//            model.addAttribute("msg","请用户登录");
//            return "/login";
//        }
        return "index";
    }
}
